import Vue from "vue";
import App from "./App.vue";
import router from "./router";
import store from "./store";

Vue.config.productionTip = false;

import index from "@/api/index";
Vue.prototype.$request = index;

Vue.filter("timeago", function (time) {
    let data = new Date(time);
    let dateTimeStamp = data.getTime();
    let min = 1000 * 60;
    let hour = min * 60;
    let day = hour * 24;
    let week = day * 7;
    let month = day * 30;
    let year = month * 12;
    let now = new Date().getTime();
    let diffValue = now - dateTimeStamp;

    let result = "";
    if (diffValue < 0) {
        result = "" + "未来";
    }
    let minC = diffValue / min;
    let hourC = diffValue / hour;
    let dayC = diffValue / day;
    let weekC = diffValue / week;
    let monthC = diffValue / month;
    let yearC = diffValue / year;

    if (yearC >= 1) {
        result = "" + parseInt(yearC) + "年前";
    } else if (monthC >= 1 && monthC < 12) {
        result = "" + parseInt(monthC) + " 个月前";
    } else if (weekC >= 1 && weekC < 5 && dayC > 6 && monthC < 1) {
        result = "" + parseInt(weekC) + "周前";
    } else if (dayC >= 1 && dayC <= 6) {
        result = "" + parseInt(dayC) + "天前";
    } else if (hourC >= 1 && hourC <= 24) {
        result = "" + parseInt(hourC) + "小时前";
    } else if (minC >= 1 && minC <= 59) {
        result = "" + parseInt(minC) + "分钟前";
    } else if (diffValue >= 0 && diffValue <= min) {
        result = "刚刚";
    }
    return result;
});

import "@/assets/font/iconfont.css";
//全局前置導航守衛
router.beforeEach((to, from, next) => {
    if (to.meta.title) {
        document.title = to.meta.title;
    }
    if (to.meta.auth) {
        let token = localStorage.getItem("h5-token");
        if (token) {
            next();
        } else {
            next({
                path: "/password",
                query: {
                    url: window.localStorage.href,
                },
            });
        }
    } else {
        next();
    }
});
new Vue({
    router,
    store,
    render: (h) => h(App),
}).$mount("#app");
